/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spring.DAO;

import com.spring.entidades.Pedido;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jerick
 */
@Repository
public class PedidoDAO  extends HibernateDaoSupport{
    
    @Autowired

    public PedidoDAO(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
    
    public List<Pedido> getListarPedido(){
        return getHibernateTemplate().find("select p from Pedido p");
    }
    
     public List<Pedido> getbuscarPedido(String numped)
     {
         if (numped.length()==1)
            numped="000"+numped;
        else if (numped.length()==2)
            numped="00"+numped;
        else if (numped.length()==3)
            numped="0"+numped;
        
         return getHibernateTemplate().findByNamedParam("select p from Pedido p where p.nroPedido=:param", "param", numped);//en jpql
         
     }
    
    public boolean insertarPedido(Pedido ped){
         boolean b=false;
     try{
         getHibernateTemplate().saveOrUpdate(ped);
         b=true;
     }
     catch(Exception e)
     {
         JOptionPane.showMessageDialog(null, e.getMessage());
     }
         return b;
     }
    
    public boolean eliminarPedido(String numped)
     {
         boolean b=false;
         try{
             Pedido t=getHibernateTemplate().get(Pedido.class, numped);
             getHibernateTemplate().delete(t);
             b=true;
             
         }
         catch (Exception e){
             b=false;
             
         }
         return b;
     }

    public String ultimo_pedido(){
        String ped = null;
        try{
            List<Pedido>    ped1 = getHibernateTemplate().find("SELECT p.nroPedido FROM Pedido p ");
            
            //JOptionPane.showMessageDialog(null, ped.getNroPedido());
            ped=""+ped1.get(ped1.size()-1);
            ped=""+(Integer.parseInt(ped)+1);
            if (ped.length()==1)
            ped="000"+ped;
        else if (ped.length()==2)
            ped="00"+ped;
        else if (ped.length()==3)
            ped="0"+ped;
        
            
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"estoy en el dao -----"+ e.getMessage());
        }
        
        
        return ped;
    }
    
}
